﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using Hartley.ApplicationLogic;
using Hartley.DTOs;
using Hartley.Util;

namespace WebApplication1
{
    public partial class viewOneScholarshipInYrWise : System.Web.UI.Page
    {
        protected int currentPageNumber = 1;
        private const int PAGE_SIZE = 10;
        List<ScholarshipDataDTO> allScholarshipData;
        string queryString;
        FundAL fundAL;
        MemberAL memberAL;
        StudentAL studentAL;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                BindData();

            }
        }



        private void BindData()
        {

            fundAL = new FundAL();
            
            allScholarshipData = new List<ScholarshipDataDTO>();
            List<ScholarshipDataDTO> allScholarshipDataOfOnePage = new List<ScholarshipDataDTO>();
            SportsUtil sportsUtil = new SportsUtil();
            queryString = Request.QueryString.ToString();
            string afterRemovedPlusSign = "";
            string afterReplaceComma = "";
            string correctedQueryString = "";
            Guid selectedScholarshipId =Guid.Empty;
            int selectedYear=0;
          
            string[] arguments = new string[5];
            arguments = queryString.Split('&');

                foreach (string argument in arguments)
                {
                    if (argument.Contains("+"))
                    {
                        afterRemovedPlusSign = argument.Replace("+", " ");
                    }
                    else
                    {
                        afterRemovedPlusSign = argument;
                    }



                    if (argument.Contains("SelectedScholarshipId"))
                    {

                        selectedScholarshipId = new Guid(afterRemovedPlusSign.Substring((afterRemovedPlusSign.IndexOf('=') + 1)));
                    }

                    if (argument.Contains("SelectedYear"))
                    {

                        selectedYear = Convert.ToInt32( sportsUtil.decryptQueryStrings(afterRemovedPlusSign.Substring(afterRemovedPlusSign.IndexOf('=') + 1)));
                    }
                }


                allScholarshipData = fundAL.GetAllScholarShipDataByScholarShipId(selectedScholarshipId).Where(a => a.amountSettledAt.Value.Year == selectedYear).ToList();
                


            // get the total rows 
                double totalRows = (int)allScholarshipData.Count;

            lblAllScholarshipPage.Text = "Displaying  the Scholarship Data for one Year - Page ";
            lblAllScholarshipOf.Text = "  of  ";

            lblAllScholarshipTotalPage.Text = CalculateTotalPages(totalRows).ToString();

            lblAllScholarshipCurrPage.Text = currentPageNumber.ToString();

            if (Convert.ToInt32(lblAllScholarshipTotalPage.Text) < 3)
            {
                if (Int32.Parse(lblAllScholarshipTotalPage.Text) <= 0)
                {
                    btnAllScholarshipFirst.Enabled = false;
                    btnAllScholarshipNext.Enabled = false;
                    btnAllScholarshipPrev.Enabled = false;
                    btnAllScholarshipLast.Enabled = false;


                }

                if (currentPageNumber == 1)
                {
                    btnAllScholarshipFirst.Enabled = false;
                    btnAllScholarshipPrev.Enabled = false;

                    if (Int32.Parse(lblAllScholarshipTotalPage.Text) <= 0)
                    {
                        btnAllScholarshipFirst.Enabled = false;
                        btnAllScholarshipPrev.Enabled = false;
                        btnAllScholarshipNext.Enabled = false;
                        btnAllScholarshipLast.Enabled = false;


                    }

                    if (Int32.Parse(lblAllScholarshipTotalPage.Text) == 1)
                    {
                        allScholarshipDataOfOnePage = allScholarshipData.GetRange(0, Convert.ToInt32(totalRows));

                        btnAllScholarshipNext.Enabled = false;
                        btnAllScholarshipLast.Enabled = false;

                    }
                    else if (Int32.Parse(lblAllScholarshipTotalPage.Text) == 2)
                    {
                        allScholarshipDataOfOnePage = allScholarshipData.GetRange(0, PAGE_SIZE);

                        btnAllScholarshipNext.Enabled = true;
                        btnAllScholarshipLast.Enabled = true;

                    }



                }

                else
                {
                    btnAllScholarshipPrev.Enabled = true;
                    btnAllScholarshipFirst.Enabled = true;

                    if (currentPageNumber == Int32.Parse(lblAllScholarshipTotalPage.Text))
                    {
                        allScholarshipDataOfOnePage = allScholarshipData.GetRange((currentPageNumber - 1) * PAGE_SIZE
                            , (Convert.ToInt32(totalRows) - (currentPageNumber - 1) * PAGE_SIZE));

                        btnAllScholarshipNext.Enabled = false;
                        btnAllScholarshipLast.Enabled = false;

                    }
                    else
                    {
                        allScholarshipDataOfOnePage = allScholarshipData.GetRange((currentPageNumber - 1) * PAGE_SIZE, PAGE_SIZE);

                        btnAllScholarshipNext.Enabled = true;
                        btnAllScholarshipLast.Enabled = true;


                    }
                }




            }
            else
            {
                if (currentPageNumber == 1)
                {
                    allScholarshipDataOfOnePage = allScholarshipData.GetRange(0, PAGE_SIZE);

                    btnAllScholarshipFirst.Enabled = false;
                    btnAllScholarshipPrev.Enabled = false;
                    btnAllScholarshipNext.Enabled = true;
                    btnAllScholarshipLast.Enabled = true;

                }



                else
                {
                    btnAllScholarshipPrev.Enabled = true;
                    btnAllScholarshipFirst.Enabled = true;

                    if (currentPageNumber == Int32.Parse(lblAllScholarshipTotalPage.Text))
                    {
                        allScholarshipDataOfOnePage = allScholarshipData.GetRange((currentPageNumber - 1) * PAGE_SIZE
                            , (Convert.ToInt32(totalRows) - (currentPageNumber - 1) * PAGE_SIZE));

                        btnAllScholarshipNext.Enabled = false;
                        btnAllScholarshipLast.Enabled = false;

                    }
                    else
                    {
                        allScholarshipDataOfOnePage = allScholarshipData.GetRange((currentPageNumber - 1) * PAGE_SIZE, PAGE_SIZE);

                        btnAllScholarshipNext.Enabled = true;
                        btnAllScholarshipLast.Enabled = true;


                    }


                }
            }

            // gvAllMeeting.DataSource = allMeetingsOfOnePage;
            // gvAllMeeting.DataBind();

            CreateDynamicTable(allScholarshipDataOfOnePage,selectedYear);


        }

        private int CalculateTotalPages(double totalRows)
        {
            int totalPages = (int)Math.Ceiling(totalRows / (PAGE_SIZE));

            return totalPages;
        }

        protected void ChangePage(object sender, CommandEventArgs e)
        {

            switch (e.CommandName)
            {

                case "First":
                    currentPageNumber = 1;
                    break;

                case "Previous":
                    currentPageNumber = Int32.Parse(lblAllScholarshipCurrPage.Text) - 1;
                    break;

                case "Next":
                    currentPageNumber = Int32.Parse(lblAllScholarshipCurrPage.Text) + 1;
                    break;
                case "Last":
                    currentPageNumber = Int32.Parse(lblAllScholarshipTotalPage.Text);
                    break;
            }


            BindData();
        }


        private void CreateDynamicTable(List<ScholarshipDataDTO> allScholarshipDataOfOnePage,int selectedYr)
        {

            List<ScholarshipDTO> scholarshipDTO = new List<ScholarshipDTO>();
            scholarshipDTO = fundAL.GetScholarShipById(allScholarshipDataOfOnePage[0].ScholarshipId);
            studentAL = new StudentAL();
            memberAL = new MemberAL();

            lblViewScholInYr1.Text = "Detailed view of ";
            hlViewScholInYr.Text = scholarshipDTO[0].Title;
            hlViewScholInYr.NavigateUrl = "viewScholarshipById.aspx?" + scholarshipDTO[0].ScholarshipId;
            lblViewScholInYr2.Text = " for the year of " + selectedYr;

            PlaceHolder4.Controls.Clear();
            PlaceHolder44.Controls.Clear();

            
            PlaceHolder44.Controls.Add(lblViewScholInYr1);
            PlaceHolder44.Controls.Add(hlViewScholInYr);
            PlaceHolder44.Controls.Add(lblViewScholInYr2);
            // Fetch the number of Rows and Columns for the table 
            // using the properties

            int tblRows = allScholarshipDataOfOnePage.Count;



            // Create a Table and set its properties 
            Table tbl = new Table();
            tbl.Width = new Unit("100%");
            tbl.BorderStyle = BorderStyle.Solid;
            // Add the table to the placeholder control
            PlaceHolder4.Controls.Add(tbl);

            TableRow trHeader = new TableRow();
            trHeader.Width = new Unit("25%");

            HyperLink hlScholCellHeaderStudentName = new HyperLink();
            hlScholCellHeaderStudentName.Text = "Student Name";

            HyperLink hlScholCellHeaderAmt = new HyperLink();
            hlScholCellHeaderAmt.Text = "Amount";

            HyperLink hlScholCellHeaderAmtSettldAt = new HyperLink();
            hlScholCellHeaderAmtSettldAt.Text = "Amount Settled On";


            HyperLink hlScholCellHeaderAmtSettldBy = new HyperLink();
            hlScholCellHeaderAmtSettldBy.Text = "Amount Settled By";

            HyperLink hlScholCellHeaderAkngdOn = new HyperLink();
            hlScholCellHeaderAkngdOn.Text = "Acknowledged On";

            HyperLink hlScholCellHeaderNotes = new HyperLink();
            hlScholCellHeaderNotes.Text = "Notes";

           
            //hlCompetitionCellHeaderMore.BackColor = System.Drawing.Color.DarkSlateBlue;
            //hlCompetitionCellHeaderMore.ForeColor = System.Drawing.Color.White;
            List<HyperLink> allHeaderHyperlinks = new List<HyperLink>();

            //based  on selected type change column orders

            allHeaderHyperlinks.Add(hlScholCellHeaderStudentName);
            allHeaderHyperlinks.Add(hlScholCellHeaderAmt);
            allHeaderHyperlinks.Add(hlScholCellHeaderAmtSettldAt);
            allHeaderHyperlinks.Add(hlScholCellHeaderAmtSettldBy);
            allHeaderHyperlinks.Add(hlScholCellHeaderAkngdOn);
            allHeaderHyperlinks.Add(hlScholCellHeaderNotes);   


            foreach (HyperLink hyperlinkHeader in allHeaderHyperlinks)
            {
                TableCell tcHeader = new TableCell();
                tcHeader.BackColor = System.Drawing.Color.DarkSlateBlue;
                tcHeader.ForeColor = System.Drawing.Color.White;

                tcHeader.Controls.Add(hyperlinkHeader);
                trHeader.Cells.Add(tcHeader);



            }

            tbl.Rows.Add(trHeader);


            // Now iterate through the table and add your controls 
            for (int i = 0; i < tblRows; i++)
            {
                TableRow tr = new TableRow();
                tr.Width = new Unit("25%");

                HyperLink hlScholCellStudentName = new HyperLink();
                hlScholCellStudentName.Text = studentAL.GetStudentById( allScholarshipDataOfOnePage[i].StudentId)[0].FullName ;
                hlScholCellStudentName.NavigateUrl = "viewAllScholarshipsReceivedByStudent.aspx?" + allScholarshipDataOfOnePage[i].StudentId;

                HyperLink hlScholCellAmt = new HyperLink();
                hlScholCellAmt.Text = allScholarshipDataOfOnePage[i].amount.ToString();

                HyperLink hlScholCellAmtSettldAt = new HyperLink();
                hlScholCellAmtSettldAt.Text = allScholarshipDataOfOnePage[i].amountSettledAt.Value.ToShortDateString();

                HyperLink hlScholCellAmtSettldBy = new HyperLink();
                hlScholCellAmtSettldBy.Text = memberAL.GetMemberById(new Guid( allScholarshipDataOfOnePage[i].SettledBy.Value.ToString()))[0].FullName;
                hlScholCellAmtSettldBy.NavigateUrl = "viewMember.aspx?" + allScholarshipDataOfOnePage[i].SettledBy;

                HyperLink hlScholCellAkngOn = new HyperLink();
                hlScholCellAkngOn.Text = allScholarshipDataOfOnePage[i].AcknowledgeAt.Value.ToShortDateString();

                HyperLink hlScholCellNotes = new HyperLink();
                hlScholCellNotes.Text = allScholarshipDataOfOnePage[i].Notes; 

                queryString = Request.QueryString.ToString();

                List<HyperLink> allHyperlinks = new List<HyperLink>();



                allHyperlinks.Add(hlScholCellStudentName);
                allHyperlinks.Add(hlScholCellAmt);
                allHyperlinks.Add(hlScholCellAmtSettldAt);
                allHyperlinks.Add(hlScholCellAmtSettldBy);
                allHyperlinks.Add(hlScholCellAkngOn);
                allHyperlinks.Add(hlScholCellNotes);

                foreach (HyperLink hyperlink in allHyperlinks)
                {
                    TableCell tc = new TableCell();



                    if (i % 2 == 0)
                    {
                        tc.Style.Add("background-color", "White");
                        tc.ForeColor = System.Drawing.Color.Blue;
                    }
                    else
                    {
                        tc.Style.Add("background-color", "#003399");
                        tc.ForeColor = System.Drawing.Color.White;

                    }



                    // Add the control to the TableCell
                    tc.Controls.Add(hyperlink);


                    // Add the TableCell to the TableRow
                    tr.Cells.Add(tc);
                }

                // Add the TableRow to the Table

                tbl.Rows.Add(tr);
            }

            // This parameter helps determine in the LoadViewState event,
            // whether to recreate the dynamic controls or not

            ViewState["dynamictable"] = true;
        }
    }
}